<template>
  <div class="bg">
    <div class="login-container">
      <div class="login-box">
        <div class="login-title">欢迎登录</div>
        <div class="login-form">
          <el-form :model="user">
            <el-form-item>
              <el-input v-model="user.name" prefix-icon="el-icon-user" style="width: 80%"
                        placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="user.password" prefix-icon="el-icon-lock" style="width: 80%" placeholder="请输入密码"
                        show-password></el-input>
            </el-form-item>
            <el-form-item>
              <template>
                <el-select v-model="radio" placeholder="请选择" style="width: 80%">
                  <el-option value="管理员"></el-option>
                  <el-option value="用户"></el-option>
                  <el-option value="员工"></el-option>
                </el-select>
              </template>
            </el-form-item>
            <el-form-item>
              <el-button style="width: 80%; margin-top: 10px" type="info" @click="login()">登录</el-button>
            </el-form-item>
            <div style="text-align: center">
              没有账号?
              <a href="javascript:void(0)" style="text-decoration: none;color: #5fafd7" @click="navRegister">点击注册</a>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'LoginView',
  data() {
    return {
      radio: '',
      user: {}
    };
  },
  methods: {
    login() {
      if (this.radio === "管理员") {
        request.post('/admin/login', this.user).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '登录成功',
              type: 'success'
            });
            //把返回的用户信息缓存到前台的页面上 为了登录成功后有显示
            localStorage.setItem("user", JSON.stringify(res.data));
            this.$router.push('/');
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        });
      }
      if (this.radio === "用户") {
        /*用户登录*/
        request.post('/user/login', this.user).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '登录成功',
              type: 'success'
            });
            //把返回的用户信息缓存到前台的页面上 为了登录成功后有显示
            localStorage.setItem("user", JSON.stringify(res.data));
            this.$router.push('/main');
          } else {
            this.$message({
              message: res.msg,
              type: 'error'
            });
          }
        });

      }
      if (this.radio === "员工") {
        request.post('/employee/login', this.user).then(res => {
          if (res.code === '0') {
            this.$message({
              message: '登陆成功',
              type: 'success'
            });
            localStorage.setItem("user", JSON.stringify(res.data));
            this.$router.push('/')
          } else {
            this.$message.error(res.msg);
          }
        })
      }
    },
    /*跳转注册界面*/
    navRegister() {
      this.$router.push('/register');
    }
  }
};
</script>

<style scoped>
.bg {
  background-image: url('../assets/images/bluedog.jpg');
  background-size: cover;
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.login-box {
  width: 400px;
  height: 370px;
  margin: 150px auto;
  background-color: rgba(134, 150, 167, 0.5);
  border-radius: 10px;
}

.login-title {
  width: 100%;
  height: 100px;
  font-size: 30px;
  line-height: 100px;
  text-align: center;
  color: #687179;
}

.login-form {
  margin-top: 25px;
  text-align: center;
  height: 320px;
}
</style>
